Golang : Reclaim memory occupied by make() example
Problem :
You have allocated some memory for buffer via the make()
function and you want to reclaim back the occupied memory after done using the buffer. How to do that?
Solution :
For example :
buffer := make([]byte, 1024)
or
buffer := make([][]int, row)
to reclaim back the memory, simply do :
buffer = nil
Golang's garbage collector will automatically free up the memory or you can trigger garbage collection manually as well with runtime/debug.FreeOSMemory()
References :
https://www.socketloop.com/tutorials/golang-how-to-get-garbage-collection-data
https://www.socketloop.com/tutorials/golang-when-to-use-make-or-new
See also : Golang : When to use make or new?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.2k CodeIgniter : How to check if a session exist in PHP?
+10.3k Golang : How to check if a website is served via HTTPS
+5.9k Golang : Generate multiplication table from an integer example
+6.3k Golang : Extract sub-strings
+6.3k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+14.8k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+4.8k PHP : Extract part of a string starting from the middle
+8.3k Swift : Convert (cast) Character to Integer?
+9k Golang : How to use Gorilla webtoolkit context package properly
+18.6k Golang : Find IP address from string
+29.3k Golang : Save map/struct to JSON or XML file
+8.3k Golang : Auto-generate reply email with text/template package